package it.gtug.torino;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class Example extends Activity {
	private Gallery gallery;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gallery = (Gallery) findViewById(R.id.examplegallery);
		gallery.setAdapter(new AddImgAdp(this));

		gallery.setOnItemClickListener(new Gallery.OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Toast.makeText(Example.this, "Position=" + position,
						Toast.LENGTH_SHORT).show();
			}
		});

	}

	public class AddImgAdp extends BaseAdapter {
		int GalItemBg;
		private Context cont;

		private Integer[] imgid = { 
				R.drawable.presentazione002,
				R.drawable.presentazione003,
				R.drawable.presentazione003a,
				R.drawable.presentazione004,
				R.drawable.presentazione005,
				R.drawable.presentazione006,
				R.drawable.presentazione007,
				R.drawable.presentazione008,
				R.drawable.presentazione009,
				R.drawable.presentazione010,
				R.drawable.presentazione011,
				R.drawable.presentazione012,
				R.drawable.presentazione013,
				R.drawable.presentazione014,
				R.drawable.presentazione015,
				R.drawable.presentazione016,
				R.drawable.presentazione017,
				R.drawable.presentazione018,
				R.drawable.presentazione019
				};

		public AddImgAdp(Context c) {
			cont = c;
			TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
			GalItemBg = typArray.getResourceId(
					R.styleable.GalleryTheme_android_galleryItemBackground, 0);
			typArray.recycle();
		}

		public int getCount() {
			return imgid.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imgView = new ImageView(cont);

			imgView.setImageResource(imgid[position]);
			imgView.setLayoutParams(new Gallery.LayoutParams(230, 350));
			imgView.setScaleType(ImageView.ScaleType.FIT_XY);
			imgView.setBackgroundResource(GalItemBg);

			return imgView;
		}
	}
}